package app;

import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import utils.ImgFileUtils;
import utils.ImgUtils;
import utils.OCRUtils;
import utils.WindowsUtils;

import java.awt.*;
import java.awt.event.InputEvent;
import java.io.File;
import java.util.concurrent.TimeUnit;

/**
 * @program: ocr_auto_app
 * @description:
 * @author: YB
 * @create: 2022-08-08 14:12
 **/
public class app {


    private static String WIN_TITLE = "视联模拟考试";
    private static String PICTURE_PATH = "D:\\auto\\picture";
    private static int A_X_BASE, SUBMIT_X_BASE, NEXT_X_BASE, A_Y_BASE, SUBMIT_Y_BASE, NEXT_Y_BASE, COUNT, A_B_CHOOSE;

    private static int INTERVAL = 20;

    static {
        Config config = ConfigFactory.load("app");
        A_X_BASE = config.getInt("APP.A_X_BASE");
        SUBMIT_X_BASE = config.getInt("APP.SUBMIT_X_BASE");
        NEXT_X_BASE = config.getInt("APP.NEXT_X_BASE");
        A_Y_BASE = config.getInt("APP.A_Y_BASE");
        SUBMIT_Y_BASE = config.getInt("APP.SUBMIT_Y_BASE");
        NEXT_Y_BASE = config.getInt("APP.NEXT_Y_BASE");
        COUNT = config.getInt("APP.COUNT") + (int) (Math.random() * 100L);
        System.out.println("A_X_BASE:" + A_X_BASE);
        System.out.println("SUBMIT_X_BASE:" + SUBMIT_X_BASE);
        System.out.println("NEXT_X_BASE:" + NEXT_X_BASE);
        System.out.println("A_Y_BASE:" + A_Y_BASE);
        System.out.println("SUBMIT_Y_BASE:" + SUBMIT_Y_BASE);
        System.out.println("NEXT_Y_BASE:" + NEXT_Y_BASE);
    }


    /**
     * 测试OCR：指定窗口截图&OCR识别
     */
    public static int testCapture() throws Exception {

        int lines = 0;
        // 截图保存的路径
        File imgFile = new File(PICTURE_PATH + "/capture.png");
        if (!imgFile.getParentFile().exists()) {
            imgFile.getParentFile().mkdirs();
        }

        // 第一个参数是Windows窗体的窗体类，第二个参数是窗体的标题。
        WinDef.HWND hWnd = User32.INSTANCE.FindWindow(null, WIN_TITLE);
        System.out.println("找到窗体：" + hWnd);

        if (hWnd == null) {
            System.out.println("未找到窗口");
            System.exit(0);
        } else {
            // 保存截图
            ImgFileUtils.saveAsPNG(ImgUtils.clipImage(WindowsUtils.captureWindowAndActive(hWnd)), imgFile); //Active解决有些窗口截图不正常
            String ocrRs = OCRUtils.doOCRFromFile(imgFile);
//            String ocrRs = OCRUtils.doOCRFromImgBuffer(ImgUtils.clipImage(ImgUtils.grayImage(WindowsUtils.captureWindowAndActive(hWnd)))); // 灰化效果
            String[] ss = ocrRs.split("\n");
            lines = 0;
            for (int i = 3; i < ss.length; i++) {
                if (ss[i].equals("") || ss[i].startsWith("A") || ss[i].startsWith(" A")) {
                    System.out.println("break");
                    break;
                }
                System.out.println(ss[i]);
                lines++;
            }
        }
        return lines;
    }

    public static void main(String[] args) {

        try {
            //设置训练库的位置
            OCRUtils.DATA_PATH = "D:\\auto\\config\\tessdata-4.1.0";
            OCRUtils.DEFAULT_LANG = "chi_sim"; //eng ：英文  chi_sim ：简体中文
            for (int i = 0; i < COUNT; i++) {
                //测试OCR：指定窗口截图&OCR识别
                WIN_TITLE = "视联模拟考试"; //用记事本打开测试文本
                int lines = testCapture();
                if (lines == 0)
                    System.out.println("本题识别失败***************************");
                else {
                    System.out.println("lines:" + lines);
                    Robot robot = new Robot();
                    if (Math.random()>0.5) A_B_CHOOSE = 70;else A_B_CHOOSE = 0;//50概率随机选AB
                    click(robot, A_X_BASE, A_Y_BASE + A_B_CHOOSE + lines * INTERVAL);//点击A/B选项
                    TimeUnit.MILLISECONDS.sleep((long) (Math.random()* 2000L));
                    click(robot, SUBMIT_X_BASE, SUBMIT_Y_BASE + lines * INTERVAL);//点击提交
                    TimeUnit.MILLISECONDS.sleep((long) (Math.random()* 1000L)+300L);
                    click(robot, NEXT_X_BASE, NEXT_Y_BASE);//点击下一题
                    TimeUnit.MILLISECONDS.sleep((long) (Math.random()* 1500L));
                }
                TimeUnit.MILLISECONDS.sleep((long) (Math.random()* 2000L));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static void click(Robot robot, int x, int y) {
        System.out.println("x:" + x + ";y:" + y);
        robot.mouseMove(x, y);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
    }
}
